package org.jdsm.gae.komarichyn.admin;

import com.opensymphony.xwork2.ActionSupport;
import org.jdsm.flow.core.users.IGroup;
import org.jdsm.flow.core.users.IRole;
import org.jdsm.flow.core.users.IUser;
import org.jdsm.flow.core.users.IUserService;
import org.jdsm.gae.komarichyn.Paging;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * User: mikom
 * Date: 11/27/12
 * Time: 3:53 PM
 */
public class PermissionAction extends ActionSupport {

    @Resource
    private IUserService userService;

    private List<IRole> roles = new ArrayList<IRole>();
    private List<IGroup> groups = new ArrayList<IGroup>();
    private List<IUser> users = new ArrayList<IUser>();


    @Resource private IUser user;
    @Resource private IRole role;
    @Resource private IGroup group;

    private Paging paging = new Paging();

    @Override
    public String execute() throws Exception {
        return SUCCESS;
    }

    public String users() throws Exception {
        users.clear();
        users.addAll(userService.loadUsersByCriteria(10, 0, false, null));
        paging.setTotal(userService.usersCount());
        return SUCCESS;
    }

    public String roles() throws Exception{
        roles.clear();
        roles.addAll(userService.loadRolesByCriteria(10,0,false,null));
        paging.setTotal(userService.rolesCount());
        return SUCCESS;
    }

    public String groups() throws Exception{
        groups.clear();
        groups.addAll(userService.loadGroupsByCriteria(10,0,false,null));
        paging.setTotal(userService.groupCount());
        return SUCCESS;
    }

    public String userRoles() throws Exception{
        roles.clear();
        Set<IRole> r = userService.findUserRoles(user);
        roles.addAll(r);
        paging.setTotal(r.size());
        return SUCCESS;
    }

    public String userGroups() throws Exception{
        groups.clear();
        Set<IGroup> g = userService.findUserGroups(user);
        groups.addAll(g);
        paging.setTotal(g.size());
        return SUCCESS;
    }


    public List<IRole> getRoles() {
        return roles;
    }

    public void setRoles(List<IRole> roles) {
        this.roles = roles;
    }

    public List<IGroup> getGroups() {
        return groups;
    }

    public void setGroups(List<IGroup> groups) {
        this.groups = groups;
    }

    public List<IUser> getUsers() {
        return users;
    }

    public void setUsers(List<IUser> users) {
        this.users = users;
    }

    public IUser getUser() {
        return user;
    }

    public void setUser(IUser user) {
        this.user = user;
    }

    public IRole getRole() {
        return role;
    }

    public void setRole(IRole role) {
        this.role = role;
    }

    public IGroup getGroup() {
        return group;
    }

    public void setGroup(IGroup group) {
        this.group = group;
    }


    public Paging getPaging() {
        return paging;
    }

    public void setPaging(Paging paging) {
        this.paging = paging;
    }
}
